package com.li.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

import com.li.utils.TransformUtil;


/**
 * Created by ZhangHang on 2016/9/30.
 */

public class CircleView extends View {
    //默认半径
    private int DEFAULT_RADIUS;
    private Context mContext;
    //半径
    private float mRadius;
    //画笔
    private Paint mPaint;
    //颜色
    private int mColor = Color.RED;

    private LinearLayout mAnimationLayout;
    private ViewGroup mRootView;

    private OnAnimationCompleteListener mOnAnimationCompleteListener;

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        //设置默认半径
        DEFAULT_RADIUS = TransformUtil.dp2px(context, 6);

        mPaint = new Paint();
        mPaint.setColor(mColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);
            mRadius = widthSize / 2;
        } else if (heightMode == MeasureSpec.EXACTLY) {
            setMeasuredDimension(heightMeasureSpec, heightMeasureSpec);
            mRadius = heightSize / 2;
        } else {
            setMeasuredDimension(DEFAULT_RADIUS * 2, DEFAULT_RADIUS * 2);
            mRadius = DEFAULT_RADIUS;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
    }

    /**
     * 开始动画
     */
    public void startAnimation(View startView, View endView) {
        //起点坐标
        int[] startP = new int[2];
        startView.getLocationInWindow(startP);
        //终点坐标
        int[] endP = new int[2];
        endView.getLocationInWindow(endP);
        parabola(mContext, startP, endP);
    }

    /**
     * 抛物线
     *
     * @param context
     * @param start_location
     * @param end_location
     */
    public void parabola(Context context, int[] start_location, int[] end_location) {
        //得到RootView
        mRootView = (ViewGroup) ((Activity) context).getWindow().getDecorView();
        //创建动画层
        mAnimationLayout = new LinearLayout(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        mAnimationLayout.setLayoutParams(lp);
        mAnimationLayout.setBackgroundResource(android.R.color.transparent);
        //把动画小球添加到动画层
        mAnimationLayout.addView(CircleView.this);
        //将动画层添加到RootView
        mRootView.addView(mAnimationLayout);

        //设置小球的位置
        LinearLayout.LayoutParams viewLp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        viewLp.leftMargin = start_location[0];
        viewLp.topMargin = start_location[1];
        CircleView.this.setLayoutParams(viewLp);

        // 计算位移
        int endX = end_location[0] - start_location[0];// 动画位移的X坐标
        int endY = end_location[1] - start_location[1];// 动画位移的y坐标
        //X轴的位移动画
        TranslateAnimation translateAnimationX = new TranslateAnimation(mRadius * 2, endX, 0, 0);
        //匀速运动
        translateAnimationX.setInterpolator(new LinearInterpolator());
        translateAnimationX.setFillAfter(true);
        //Y轴位移动画
        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, mRadius * 2, endY);
        //加速度为1.7的匀加速运动
        translateAnimationY.setInterpolator(new AccelerateInterpolator(1.7f));
        translateAnimationX.setFillAfter(true);

        //将横纵坐标的位移动画添加到动画集中
        AnimationSet set = new AnimationSet(false);
        set.setFillAfter(false);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);
        set.setDuration(400);// 动画的执行时间
        //开始播放动画
        CircleView.this.startAnimation(set);
        // 动画监听事件
        set.setAnimationListener(new Animation.AnimationListener() {
            // 动画的开始
            @Override
            public void onAnimationStart(Animation animation) {
            }

            //重复播放
            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            // 动画的结束
            @Override
            public void onAnimationEnd(Animation animation) {
                mAnimationLayout.setVisibility(View.GONE);
                new Handler().post(new Runnable() {
                    public void run() {
                        mRootView.removeView(mAnimationLayout);
                    }
                });
                if (mOnAnimationCompleteListener != null) {
                    mOnAnimationCompleteListener.onAnimationComplete(animation);
                }
            }
        });
    }

    public void setOnAnimationCompleteListener(OnAnimationCompleteListener onAnimationCompleteListener) {
        mOnAnimationCompleteListener = onAnimationCompleteListener;
    }

    public interface OnAnimationCompleteListener {
        void onAnimationComplete(Animation animation);
    }
}
